<template>
  <div class="lineAndBarInfo">
    <div class="lineAndBarInfoWrap">
      <div id="lineAndBarChart"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      newStudentId: "",
      advancedInfo: [],
      myChart: null,
    };
  },
  props: ["studentId"],
  watch: {
    studentId: {
      immediate: true, // 立即触发一次
      handler(newValue) {
        if (newValue) {
          this.getAdvancedInfoById(newValue);
        }
      },
    },
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = document.getElementById("lineAndBarChart");
      this.chart = echarts.init(chartDom, "dark");
    },
    dataChart() {
      if (!this.chart) {
        this.initChart();
      }

      let dataList = this.advancedInfo.map((item) => ({
        name: item.mission_type,
        value: item.overall_avg_score,
      }));

      // 提取数据用于图表
      const xAxisData = dataList.map((item) => item.name);
      const seriesData = dataList.map((item) => item.value);
      // 计算任务间的分数差值
      const compareData = seriesData.map((value, index) => {
        if (index === 0) return 0;
        const prevValue = seriesData[index - 1];
        const scoreDiff = (value - prevValue).toFixed(2);
        return parseFloat(scoreDiff);
      });

      var option = {
        backgroundColor: "transparent",
        grid: {
          top: "20%",
          bottom: "20%", 
          left: "1%",
          right: "1%",
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0, 24, 48, 0.8)",
          borderColor: "#00FFE3",
          borderWidth: 1,
          padding: [8, 12],
          textStyle: {
            color: "#ffffff",
            fontSize: 14,
            fontWeight: 'bold'
          },
          formatter: function(params) {
            let result = '<div style="border-bottom: 1px solid #00FFE3; padding-bottom: 5px; margin-bottom: 5px;">' + 
                        params[0].name + '</div>';
            params.forEach(item => {
              if (item.seriesName === '任务得分') {
                result += '<div style="margin: 3px 0;">' + 
                         '<span style="display: inline-block; width: 10px; height: 10px; background: #00FFE3; margin-right: 5px;"></span>' +
                         item.seriesName + ': <span style="color: #00FFE3;">' + item.value + '分</span></div>';
              } else {
                const value = parseFloat(item.value);
                const prefix = value > 0 ? '+' : '';
                const color = value > 0 ? '#00FFE3' : '#4D9FFF';
                result += '<div style="margin: 3px 0;">' + 
                         '<span style="display: inline-block; width: 10px; height: 10px; background: #058cff; margin-right: 5px;"></span>' +
                         item.seriesName + ': <span style="color: ' + color + ';">' + prefix + value + '分</span></div>';
              }
            });
            return result;
          },
          axisPointer: {
            type: "shadow",
            label: {
              show: true,
            },
          },
        },
        legend: {
          data: ["任务得分", "分数变化"],
          textStyle: {
            color: "#ffffff",
          },
        },
        xAxis: {
          data: xAxisData,
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.3)",
            },
          },
          axisTick: {
            show: true,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.3)"
            }
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: "#FFFFFF",
            },
          },
        },
        yAxis: [
          {
            type: "value",
            name: "得分",
            nameTextStyle: {
              color: "#FFFFFF",
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: true,
              lineStyle: {
                color: "rgba(255, 255, 255, 0.3)"
              }
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(255, 255, 255, 0.3)",
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#FFFFFF",
              },
            },
          },
          {
            type: "value",
            name: "分数变化",
            nameTextStyle: {
              color: "#FFFFFF",
            },
            position: "right",
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              formatter: function(value) {
                return value > 0 ? '+' + value : value;
              },
              textStyle: {
                color: "#FFFFFF",
              },
            },
          },
          {
            type: "value",
            gridIndex: 0,
            min: 50,
            max: 100,
            splitNumber: 8,
            splitLine: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: true,
              areaStyle: {
                color: ["rgba(250,250,250,0.0)", "rgba(250,250,250,0.05)"],
              },
            },
          },
        ],
        series: [
          {
            name: "分数变化",
            type: "line",
            yAxisIndex: 1,
            smooth: true,
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 10,
            itemStyle: {
              color: "#058cff",
            },
            lineStyle: {
              color: "#058cff",
            },
            areaStyle: {
              color: "rgba(5,140,255, 0.2)",
            },
            data: compareData,
          },
          {
            name: "任务得分",
            type: "bar",
            barWidth: 25,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(0, 255, 255, 0.8)',
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(0, 255, 255, 0.4)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(0, 255, 255, 0.1)',
                  }
                ]),
                borderRadius: [15, 15, 0, 0],
                shadowColor: 'rgba(0, 255, 255, 0.3)',
                shadowBlur: 20,
                shadowOffsetY: 5,
                borderWidth: 1,
                borderColor: 'rgba(255, 255, 255, 0.3)',
                opacity: 0.8
              },
            },
            emphasis: {
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(0, 255, 255, 0.9)',
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(0, 255, 255, 0.5)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(0, 255, 255, 0.2)',
                  }
                ]),
                shadowColor: 'rgba(0, 255, 255, 0.5)',
                shadowBlur: 30,
                shadowOffsetY: 8,
                borderWidth: 2,
                borderColor: 'rgba(255, 255, 255, 0.5)',
                opacity: 1
              }
            },
            data: seriesData,
          },
        ],
      };

      this.chart.setOption(option);
    },
    getAdvancedInfoById(id) {
      this.$axios
        .get("/students/getAdvancedInfoById/" + id)
        .then((res) => {
          this.advancedInfo = [...res.data.data]
            .map((item) => ({
              ...item,
              mission_type: "任务" + Number(item.mission_type),
            }))
            .sort((a, b) => {
              return (
                Number(a.mission_type.replace("任务", "")) -
                Number(b.mission_type.replace("任务", ""))
              );
            });
          this.dataChart();
        });
    },
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
  },
};
</script>
<style scoped>
.lineAndBarInfo {
  width: 100%;
  height: 100%;
}

.lineAndBarInfoWrap {
  display: flex;
  justify-content: center;
  flex-direction: row;
  background-color: rgba(255, 255, 255, 0);
  height: 95%;
  width: 100%;
  margin-bottom: 5%;
}

#lineAndBarChart {
  width: 100%;
  height: 100%;
  padding: 0;
  box-sizing: border-box;
}
</style>